﻿using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_RawMaterialReceiveBusiness : BaseBusiness<PB_RawMaterialReceive>, IPB_RawMaterialReceiveBusiness, ITransientDependency
    {
        public PB_RawMaterialReceiveBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<PB_RawMaterialReceive>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_RawMaterialReceive>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_RawMaterialReceive, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }
        /// <summary>
        /// 材料耗用流水
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<PB_RawMaterialReceiveDTO>> GetDataDTOListAsync(PageInput<PB_RawMaterialReceiveConditionDTO> input)
        {
            string str = @"SELECT a.CollectionTime,b.id as TaskCode,b.poname as TaskName,d.Code as ProductCode,d.Name as ProductName,d.Spec as ProductSpec,f.name as SubordinateProductName,c.name as Procedurename,a.Nums,e.RealName as creator from PB_RawMaterialReceive  a left join PB_ProductionOrder b on a.OrderId=b.Id
            left join PB_ProcecureSub c on a.ProcedureId=c.id
            left join PB_Material d on a.MaterialId=d.Id
            left join Base_User e on a.CreatorId=e.id
            left join PB_Material f on a.SubordinateProduct=f.Id where 1=1 ";
            if (!input.Search.TaskCode.IsNullOrEmpty())
            {
                str += " and b.id like '%" + input.Search.TaskCode + "%' ";
            }
            if (!input.Search.TaskName.IsNullOrEmpty())
            {
                str += " and b.POName like '%" + input.Search.TaskName + "%' ";
            }
            if (!input.Search.ProductName.IsNullOrEmpty())
            {
                str += " and d.name like '%" + input.Search.ProductName + "%' ";
            }
            if (!input.Search.SubordinateProductName.IsNullOrEmpty())
            {
                str += " and f.name like '%" + input.Search.SubordinateProductName + "%' ";
            }
            if (!input.Search.ProcedureName.IsNullOrEmpty())
            {
                str += " and c.name like '%" + input.Search.ProcedureName + "%' ";
            }
            if (!input.Search.ProcedureId.IsNullOrEmpty())
            {
                str += " and a.ProcedureId like '%" + input.Search.ProcedureId + "%' ";
            }
            //if (!input.Search.StartDate.IsNullOrEmpty() && !input.Search.EndDate.IsNullOrEmpty())
            //{
            //    str += " and DATE_FORMAT(a.CollectionTime,'%Y-%m-%d')>='" + input.Search.StartDate + "' and DATE_FORMAT(a.CollectionTime,'%Y-%m-%d')<='" + input.Search.EndDate + "' ";
            //}
            if (!input.Search.StartDate.IsNullOrEmpty())
            {
                str += " and a.CollectionTime>='" + Convert.ToDateTime(input.Search.StartDate).ToString("yyyy-MM-dd 00:00:00") + "'";
            }
            if (!input.Search.EndDate.IsNullOrEmpty())
            {
                str += " and a.CollectionTime<='" + Convert.ToDateTime(input.Search.EndDate).ToString("yyyy-MM-dd 23:59:59") + "'";
            }

            if (!input.Search.Creator.IsNullOrEmpty())
            {
                str += " and e.RealName like '%" + input.Search.Creator + "%' ";
            }
            str += " order by "+input.SortField+"  "+input.SortType+"";
            var lidata = await Db.GetListBySqlAsync<PB_RawMaterialReceiveDTO>(str);
            int num = lidata.Count;
            var list = lidata
               .Skip((input.PageIndex - 1) * input.PageRows)
               .Take(input.PageRows).ToList();
            return new PageResult<PB_RawMaterialReceiveDTO> { Data = list, Total = num };
        }
        /// <summary>
        /// 材料耗用统计
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<PB_RawMaterialReceiveDTO>> GetDataTotalDTOListAsync(PageInput<PB_RawMaterialReceiveConditionDTO> input)
        {
            string str = @"SELECT DATE_FORMAT(a.CollectionTime,'%Y-%m-%d') as CollectionTime,b.id as TaskCode,b.poname as TaskName,d.Code as ProductCode,d.Name as ProductName,d.Spec as ProductSpec,f.name as SubordinateProductName,c.name as Procedurename,sum(a.Nums) as Nums from PB_RawMaterialReceive  a left join PB_ProductionOrder b on a.OrderId=b.Id
            left join PB_ProcecureSub c on a.ProcedureId=c.id
            left join PB_Material d on a.MaterialId=d.Id
            left join PB_Material f on a.SubordinateProduct=f.Id where 1=1 ";
            if (!input.Search.TaskCode.IsNullOrEmpty())
            {
                str += " and b.id like '%" + input.Search.TaskCode + "%' ";
            }
            if (!input.Search.TaskName.IsNullOrEmpty())
            {
                str += " and b.POName like '%" + input.Search.TaskName + "%' ";
            }
            if (!input.Search.ProductName.IsNullOrEmpty())
            {
                str += " and d.name like '%" + input.Search.ProductName + "%' ";
            }
            if (!input.Search.ProcedureId.IsNullOrEmpty())
            {
                str += " and a.ProcedureId like '%" + input.Search.ProcedureId + "%' ";
            }
            if (!input.Search.SubordinateProductName.IsNullOrEmpty())
            {
                str += " and f.name like '%" + input.Search.SubordinateProductName + "%' ";
            }
            if (!input.Search.ProcedureName.IsNullOrEmpty())
            {
                str += " and c.name like '%" + input.Search.ProcedureName + "%' ";
            }
            //if (!input.Search.StartDate.IsNullOrEmpty() && !input.Search.EndDate.IsNullOrEmpty())
            //{
            //    str += " and DATE_FORMAT(a.CollectionTime,'%Y-%m-%d')<='" + input.Search.StartDate + "' and DATE_FORMAT(a.CollectionTime,'%Y-%m-%d')>='" + input.Search.EndDate + "' ";
            //}
            if (!input.Search.StartDate.IsNullOrEmpty())
            {
                str += " and a.CollectionTime>='" + Convert.ToDateTime(input.Search.StartDate).ToString("yyyy-MM-dd 00:00:00") + "'";
            }
            if (!input.Search.EndDate.IsNullOrEmpty())
            {
                str += " and a.CollectionTime<='" + Convert.ToDateTime(input.Search.EndDate).ToString("yyyy-MM-dd 23:59:59") + "'";
            }
            str += " GROUP BY DATE_FORMAT(a.CollectionTime,'%Y-%m-%d'),b.id,b.poname,d.Code ,d.Name ,d.Spec,f.name ,c.name";
            str += "  order by "+input.SortField+" "+input.SortType+" ";
            var lidata = await Db.GetListBySqlAsync<PB_RawMaterialReceiveDTO>(str);
            int num = lidata.Count;
            var list = lidata
               .Skip((input.PageIndex - 1) * input.PageRows)
               .Take(input.PageRows).ToList();
            return new PageResult<PB_RawMaterialReceiveDTO> { Data = list, Total = num };
        }
        public async Task<PB_RawMaterialReceive> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(PB_RawMaterialReceive data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(PB_RawMaterialReceive data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}